package cn.com.czpb.client.module.personal.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import cn.com.czpb.client.R;
import cn.com.czpb.client.model.usercenter.MessageBean;
import cn.com.czpb.client.module.personal.acticity.PersonPageActivity;
import cn.com.czpb.client.utils.SPUtil;
import cn.com.czpb.client.utils.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/7.
 */

public class MessageAdapter extends BaseMultiItemQuickAdapter<MessageBean, BaseViewHolder> {
    SPUtil spu;
    ReplyAdapter adapter;
    List<MessageBean> list;
    Context context;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MessageAdapter(List<MessageBean> data, Context context) {
        super(data);
        this.context=context;
        spu = SPUtil.getInstance();
        list = new ArrayList<>();
        addItemType(MessageBean.TEXT_TYPE, R.layout.item_message);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MessageBean item) {

        switch (helper.getItemViewType()) {
            case 0:
                Glide.with(mContext).load(spu.getUser().getAvatar_path()).into((ImageView) helper.getView(R.id.iv_user_message));
                helper.setText(R.id.tv_name_message, TextUtils.getLengthText(spu.getUser().getNickname(),8));
                helper.setText(R.id.tv_content_message, item.getReply());
                helper.setText(R.id.time_message, item.getCreate_time());

                helper.addOnClickListener(R.id.reply_message);

                helper.addOnClickListener(R.id.tv_title);
                Glide.with(mContext).load(item.getAvatar_path()).into((ImageView) helper.getView(R.id.iv_user_message));
                helper.getView(R.id.ll_item_message).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        if ("0".equals(item.getMediatype())) {
                            intent.setClass(mContext, PersonPageActivity.class);
                        } else if ("1".equals(item.getMediatype())) {
                            intent.setClass(mContext, PersonPageActivity.class);
                        }
                        intent.putExtra("uid", item.getUid());

                        mContext.startActivity(intent);
                    }
                });
//                helper.getView(R.id.rl_reply_message).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//
//                        ReplyItemClick(item);
//                    }
//                });
//                helper.getView(R.id.tv_content_message).setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View view) {
//                        TextView tv = helper.getView(R.id.tv_content_message);
//                        tv.setBackgroundColor(mContext.getResources().getColor(R.color.color_d4d4d4));
//                        String msg = tv.getText().toString();
//                        CommPop cp = new CommPop((Activity) context, msg, tv, 0);
//                        cp.showAsPullUp(tv, 0, 0);
//
//                        return true;
//                    }
//
//                });
                if (item.getAction().equals("3")) {
                    helper.getView(R.id.tv_title).setVisibility(View.GONE);
                    helper.setNestView(R.id.item_message);
                    RecyclerView recyclerView = helper.getView(R.id.recycler_item_message);
                    recyclerView.setVisibility(View.VISIBLE);
                    recyclerView.setLayoutManager(new LinearLayoutManager(helper.itemView.getContext(), LinearLayoutManager.VERTICAL, false));
                    adapter = new ReplyAdapter(list);
                    list.clear();
                    list.add(item);
                    setListData(list);
                    recyclerView.setAdapter(adapter);
                }else {
                    helper.getView(R.id.recycler_item_message).setVisibility(View.GONE);
                    helper.getView(R.id.tv_title).setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_title,"原文："+item.getReplyto().getTitle());
                }
                break;
        }
    }

    public void setListData(List<MessageBean> list) {

        adapter.setNewData(list);
        adapter.notifyDataSetChanged();

    }


}
